let login_id='';
let login_token='';
//读取缓存中的数据
let loginDataStr=wx.getStorageSync('login');
if(!!loginDataStr){
    let loginObj=JSON.parse(loginDataStr);
    login_id=loginObj.login_id;
    login_token=loginObj.login_token;
}
let http={
    // imageUrl:'http://127.0.0.1:8000',
    //rootUrl:'http://127.0.0.1:8000',
    //shopId:'5f2ab28c13bc3d39bcd60523',
    imageUrl:'https://jianshi.073c.com',
    rootUrl:'https://jianshi.073c.com',
    get(router,data,cb){
        let that=this;
        wx.request({
            url: `${that.rootUrl}${router}`,
            data: data,
            success(data){
                cb(data);
            }
        })
    },
    hasUserData(){
        return !!login_id;
    },
    setUserData(_id,_token){
        login_id=_id;
        login_token=_token;
        wx.setStorageSync('login',JSON.stringify({login_id:login_id,login_token:login_token}));
    },
    getUserData(){
        return {login_id:login_id,login_token:login_token};
    },
    userRequest(router,data,cb,method='get',loaddingText='获取数据中'){
        let that=this;
        let getData=function(){
            wx.request({
                url: `${that.rootUrl}${router}`,
                data: data,
                header:{login_id,login_token},
                method:method,
                success(data){
                    wx.hideLoading();
                    if(data.data.code==200){
                        cb(data.data.data);
                    }else if(data.data.code==600){
                        wx.clearStorageSync('login');
                        login_id='';
                        login_token='';
                        setTimeout(()=>{
                            that.userRequest(router,data,cb,method,loaddingText);
                        },2000);
                    }else{
                        wx.showToast({
                            title: data.data.msg,
                            icon: 'none',
                            duration: 2000
                        })
                    }
                },
				fail(){
					console.log('请求超时，5秒后重试。');
					setTimeout(()=>{
						http.userRequest(router,data,cb,method,loaddingText);
					},5000);
				}
            })
        }
        if(!login_id){
            let that=this;
            wx.showLoading({
                title: '验证身份中...',
                mask:true,
                success:()=>{
                    wx.login({
                        success: res => {
                            that.get('/weChatLogin', {
								code: res.code
							},
							function(userData){
								wx.hideLoading();
								if(userData.data.code==200){
									console.log(userData.data);
									that.setUserData(userData.data.data.login_id,userData.data.data.login_token);
									getData();
								}else{
									console.error(data.data.msg);
								}
							});
                        }
                    })
                }
            });
        }else{
            wx.showLoading({
                title: loaddingText,
                mask:true,
                success:()=>{
                    getData();
                }
            });
        }

    }
};
export default http;